﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace DemoGame1
{
    public class ChooseCharacter : Menu
    {

        KeyboardState oldState;
        public static Color Normal = Color.White;
        public static Color Dark = Color.BlueViolet ;
        int _menuItems = 0;

        MyButton choose;

        private int selectedIndex = 0;
        myGame game;

        public ChooseCharacter(myGame _g, String strXML)
        {
            game = _g;
            myButtons = new List<MyButton>();
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            //add char
            XmlElement buttons = (XmlElement)doc.SelectSingleNode("//Buttons");
            foreach (XmlNode button in buttons.ChildNodes)
            {
                MyButton bt = new MyButton(game.Content, (XmlElement)button);
                myButtons.Add(bt);
            }
            choose = myButtons[myButtons.Count - 1];
            myButtons.RemoveAt(myButtons.Count - 1);
            _menuItems = myButtons.Count / 3;
        }

        public override void HandleKeyInput()
        {
            KeyboardState newState = Keyboard.GetState();
            if (newState.IsKeyDown(Keys.Right))
            {
                if (!oldState.IsKeyDown(Keys.Right))
                {
                    selectedIndex = (selectedIndex + 1) % _menuItems;
                }
            }

            if (newState.IsKeyDown(Keys.Left))
            {
                if (!oldState.IsKeyDown(Keys.Left))
                {
                    selectedIndex = (selectedIndex + _menuItems - 1) % _menuItems;
                }
            }

            if (newState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    myGame.currentScreen = CurrentScreen.InGame;
                    string characterName = "";
                    if (selectedIndex == 1)
                        characterName = "Ken";
                    else
                        characterName = "Ryu";
                    myGame.SaveGame((1).ToString(), characterName);
                    Global.BroadcastEvent(Global.EVENTS.GM_CHARACTER_CHOSEN, this, characterName);
                }

            }

            if (newState.IsKeyDown(Keys.Back))
            {
                if (!oldState.IsKeyDown(Keys.Back))
                {
                    myGame.currentScreen = CurrentScreen.StartMenu;
                }

            }
            oldState = newState;
        }

        public override void Update(GameTime gameTime)
        {
            HandleKeyInput();

            if (selectedIndex == 0)
            {
                myButtons[4].Update(gameTime);
                myButtons[4].Sprites[0].Color = Normal;
                myButtons[5].Sprites[0].Color = Dark;
            }

            if (selectedIndex == 1)
            {
                myButtons[5].Update(gameTime);
                myButtons[5].Sprites[0].Color = Normal;
                myButtons[4].Sprites[0].Color = Dark;
            }

        }

        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            if (selectedIndex == 0)
            {
                myButtons[0].Draw(sb, gameTime);
            }

            if (selectedIndex == 1)
            {
                myButtons[1].Draw(sb, gameTime);
            }

            for (int i = 2; i < myButtons.Count; i++)
            {
                myButtons[i].Draw(sb, gameTime);
            }

            //draw string
            choose.Draw(sb, gameTime);
        }
    }
}
